import wxcharts from '../../API/wxcharts.js'//引入
Page({
  data: {
    show: false,
    list: '',
    dqBMI: 0,
    weight: 0,
    BMI: 0,
    height: 0,
    heights:{},
    bmischarge: [0, 0, 0, 0, 0],
    timescharge: ['0000-00-00', '0000-00-00', '0000-00-00', '0000-00-00', '0000-00-00'],
    user_id: '',
    autoBMI: 'BMI:自动计算',
    //data中配置
    ec: {
      lazyLoad: true
    }

  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  onClick: (event) => {
    let index = event.detail.index
    let url = ['weight', 'BMI', 'height']
    wx.navigateTo({
      url: `/pages/${url[index]}/${url[index]}`,
    })
    // console.log(title)
  },
  postdata:function(e) {
    var app = getApp()
    var ip = app.globalData.ServerAddress
    this.setData({
      user_id: app.globalData.user_id
    })
    var app = getApp()
    var ip = app.globalData.ServerAddress
    let height = e.detail.value.height
    let weight = e.detail.value.weight
    console.log(height)
    console.log(weight)
    wx.request({
      url: "http://" + ip + "/user/posture_recording",
      method: 'PUT',
      dataType: "json",
      header: {
        'Content-type': 'application/json',
        "authorization": app.globalData.token
      },
      data:{
        "user_id": this.data.user_id,
        "weight": weight,
        "height": height
      },
      success:function (res){
        console.log("成功！")
        that.onLoad()
      }
    })
  },
  onLoad() {
    var app = getApp()
    var ip = app.globalData.ServerAddress
    console.log(ip)
    let that = this
    wx.request({
      url: "http://" + ip + "/user/posture_recording",
        method: 'POST',
        dataType: "json",
        header: {
          'Content-type': 'application/json',
          
        "authorization": app.globalData.token
        },
        data: {
          "user_id": app.globalData.user_id,
        },
        success: function (res) {
          let ll = res.data.PostureRecording
          if(ll.length>0) {
            that.setData({
            // list: res.data.PostureRecording.length,
            heights: res.data.PostureRecording,
            weight: res.data.PostureRecording[0].weight,
            height: res.data.PostureRecording[0].height ,
            BMI: (res.data.PostureRecording[0].weight / (res.data.PostureRecording[0].height * res.data.PostureRecording[0].height * 0.0001)).toFixed(1),
            dqBMI: (res.data.PostureRecording[0].weight / (res.data.PostureRecording[0].height * res.data.PostureRecording[0].height * 0.0001)).toFixed(1),
          })
          let arr = []
        for(let i = ll.length - 1; i >= 0; i--) {
          let f = (ll[i].weight / (ll[i].height * ll[i].height * 0.0001))
          // fomatFloat(f,1)
          that.data.bmischarge.push(Math.floor(f*10)/10)
          console.log(that.data.bmischarge)
          that.data.timescharge.push(ll[i].time.slice(0, 10))
          console.log(that.data.timescharge)
        }
          new wxcharts({
            canvasId: 'lineCanvas',
            extra: {
              lineStyle: 'curve'
            },
            type: 'line',
            categories: [that.data.timescharge[that.data.timescharge.length - 5], that.data.timescharge[that.data.timescharge.length - 4], that.data.timescharge[that.data.timescharge.length - 3], that.data.timescharge[that.data.timescharge.length - 2], that.data.timescharge[that.data.timescharge.length - 1]],
            series: [{
                name: 'BMI',
                data: [that.data.bmischarge[that.data.bmischarge.length - 5], that.data.bmischarge[that.data.bmischarge.length - 4], that.data.bmischarge[that.data.bmischarge.length - 3], that.data.bmischarge[that.data.bmischarge.length - 2], that.data.bmischarge[that.data.bmischarge.length - 1]],
                format: function (val) {
                    return val.toFixed(1);
                }
            }
            // , {
            //     name: '成交量2',
            //     data: [0.30, 0.37, 0.65, 0.78, 0.69, 0.94],
            //     format: function (val) {
            //         return val.toFixed(2) + '万';
            //     }
            // }
          ],
            yAxis: {
                // title: 'BMI',
                format: function (val) {
                    return val.toFixed(2);
                },
                min: 0
            },
            width: 380,
            height: 300,
        });
          } 
          
        }
    });
  
  
  }
})
